package com.robot.console.util;

import java.util.Random;

public class DefaultValidate implements Validate
{
	// ct 13
	// 8041e263-77d5-4ae0-bc88-fa1b1a7f2b06
	// 8 4 4 4 12
	// 8985b8c7a531788cfab931898a64cdf1
	// 102dd980e6420e2e87c84e8b85d013d1
	// Ml1A&Yx<D5Q8-5gY/KpxrK@z^;O+n[uIpW"h:JN;dt4/P=:44cy@`Cfn)z^8=eAt
	private final byte[] publicKey = { 80, 75, 3, 4, 20, 0, 8, 0, 8, 0, -41, -124, 73, 57, 0, 0, 0, 0, 0, 0, 0, 0, 0,
			0, 0, 0, 1, 0, 0, 0, 48, 75, 52, 52, 79, 75, -77, -76, -76, 52, 51, 51, 55, 49, 55, 72, 54, 55, 51, 51, 51,
			76, 54, 77, 77, -79, -80, 76, 54, 73, 50, 54, -75, 4, 0, 80, 75, 7, 8, -126, 109, 70, -46, 34, 0, 0, 0, 32,
			0, 0, 0 };
	private final byte[] privateKey = { 80, 75, 3, 4, 20, 0, 8, 0, 8, 0, -28, -124, 73, 57, 0, 0, 0, 0, 0, 0, 0, 0, 0,
			0, 0, 0, 1, 0, 0, 0, 48, 51, 49, 50, 79, 75, 75, 49, 77, 49, -79, 48, 73, 53, 52, 48, 53, 52, 55, 48, 52,
			78, -78, 72, -78, 72, 49, 78, 76, 53, -78, 52, 51, 4, 0, 80, 75, 7, 8, 92, 29, 44, -64, 34, 0, 0, 0, 32, 0,
			0, 0 };
	private final byte[] hashKeyBytes = { 80, 75, 3, 4, 20, 0, 8, 0, 8, 0, -42, -118, 73, 57, 0, 0, 0, 0, 0, 0, 0, 0,
			0, 0, 0, 0, 1, 0, 0, 0, 48, 115, 52, 50, 114, 82, -117, -84, -80, 113, 49, 13, -76, -48, 53, 77, -113, -44,
			-9, 46, -88, 40, -14, 118, 72, -119, -77, -10, -41, -51, -117, 46, -11, 44, 8, 87, -54, -80, -14, -14, -77,
			78, 41, 49, -47, 15, -80, -75, -86, -56, 74, -82, 116, 72, 112, 78, -53, -45, -84, -118, -77, -80, 77, 77,
			42, 1, 0, 80, 75, 7, 8, -113, -11, -31, -18, 66, 0, 0, 0, 64, 0, 0, 0 };
	private String hashKey;
	private final RSA rsa = new RSA();

	public DefaultValidate()
	{
		this.initKeys(publicKey, privateKey, hashKeyBytes);
	}

	// 4,13,8,12,4,4
	// 2,ct(5),0,4,1,3
	public String decrypt(String s)
	{
		String temp = rsa.privateDecode(s);
		StringBuffer keyBuffer = new StringBuffer();
		keyBuffer.append(temp.substring(17, 25));
		keyBuffer.append('-');
		keyBuffer.append(temp.substring(37, 41));
		keyBuffer.append('-');
		keyBuffer.append(temp.substring(0, 4));
		keyBuffer.append('-');
		keyBuffer.append(temp.substring(41, 45));
		keyBuffer.append('-');
		keyBuffer.append(temp.substring(25, 37));
		String key = keyBuffer.toString();
		return key;
	}

	// 4,12,8,13,4,4
	// 1,4,0,ct(5),3,2
	public String encrypt(String s)
	{
		long ct = System.currentTimeMillis();
		String temp = s;
		String[] tempArray = temp.split("-");
		StringBuffer keyBuffer = new StringBuffer();
		keyBuffer.append(tempArray[1]);
		keyBuffer.append(tempArray[4]);
		keyBuffer.append(tempArray[0]);
		keyBuffer.append(ct);
		keyBuffer.append(tempArray[3]);
		keyBuffer.append(tempArray[2]);
		String key = this.hashEncode(keyBuffer.toString());
		return key;
	}

	public long generatorRandomSequenceId()
	{
		long sequenceId = 0L;
		Random r = new Random(System.currentTimeMillis());
		sequenceId = Math.abs(r.nextLong() % 1000000);
		return sequenceId;
	}

	//
	// public static void main(String args[])
	// {
	// AbstractValidate a = new AbstractValidate();
	// String key = UUIDGenerator.getUUID();
	// long ct = System.currentTimeMillis();
	// // for (int i = 0; i < 100000; i++)
	// // {
	// a.encrypt(key);
	// // }
	// ct = System.currentTimeMillis() - ct;
	// System.out.println(ct);
	// }

	public String hashDecode(String s)
	{
		String result = "";
		try
		{
			result = rsa.hashDecode(s, hashKey);
		}
		catch (Exception ex)
		{
			result = "";
		}
		return result;
	}

	public String hashEncode(String s)
	{
		return rsa.hashEncode(s, hashKey);
	}

	protected void initKeys(byte[] publicKey, byte[] privateKey, byte[] hashKeyBytes)
	{
		this.rsa.setPublicKey(Zip.decompressBytesByZIP(publicKey));
		this.rsa.setPrivateKey(Zip.decompressBytesByZIP(privateKey));
		this.hashKey = Zip.decompressBytesByZIP(hashKeyBytes);
	}
}
